package xyz.xtt.admin.vo;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import java.util.List;

public class ExcelDataListener implements ReadListener<ExcelData> {
    private static final int BATCH_COUNT = 100;
    private List<ExcelData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(ExcelData data, AnalysisContext context) {
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
    }

    private void saveData() {
        for (ExcelData data : cachedDataList) {
            System.out.println(data.getText());
        }
    }

    // 获取读取到的所有数据
    public List<ExcelData> getDataList() {
        return cachedDataList;
    }
}